package com.ajou.adonate.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ajou.adonate.imap.MailReceiver;

/**
 * Servlet implementation class FileDownloadServlet
 */
@WebServlet("/FileDownloadServlet")
public class FileDownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileDownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String movie_id = request.getParameter("movie_id");
		
		MailReceiver r = new MailReceiver(movie_id);
		String streamPath = r.receive();
		
		System.out.println("FileDownloadServlet.java - doGet()");
		System.out.println("Streaming Path : " + streamPath);
		
//		String fileName = request.getParameter("fileName");
//	    String filePath = request.getParameter("filePath"); 

		try {
			String agentType = request.getHeader("User-Agent");
  
			if(agentType.indexOf("MSIE")>= 0) {
				response.setContentType("application/octet-stream/x-msdownload"); 
				response.setHeader("Content-Disposition","attachment; filename=" + new String(streamPath.getBytes("euc-kr"), "8859_1"));                
			} else if(agentType.indexOf("Mozilla")>= 0) {
				response.setContentType("doesn/matter"); 
				response.setHeader("Content-Disposition","attachment; filename=" + new String(streamPath.getBytes("euc-kr"), "8859_1"));
			} else{
			}
  
			File file = new File (streamPath);
			FileInputStream filestream = new FileInputStream(file);
//	            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
			OutputStream outputStream = response.getOutputStream();

			byte[] buffer = new byte[2048];
			int readcount = 0;
			
			while((readcount = filestream.read(buffer))!=-1) {
//	                byteStream.write(buffer,0,readcount);
				outputStream.write(buffer,0,readcount);
				outputStream.flush();
			}

//	            OutputStream outStream = response.getOutputStream(); 
//	            outStream.write(byteStream.toByteArray());
//	            outStream.close();
			filestream.close();
			outputStream.close();        
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
